package service

import (
	"errors"
	"go_batch_create_orders/admin/model"
	"go_batch_create_orders/common"
	"go_batch_create_orders/utils"
)

type SpecService struct {
	utils.Db
}

func (s *SpecService) AddItem(model *model.SpecItem) (res int64, err error) {
	model.CreateTime = common.Time()
	res, err = s.getItemByWhere("item_name = '" + model.ItemName + "' and category_id = " + common.Int64ToString(model.CategoryId))
	if err != nil {
		return
	}
	if res > 0 {
		err = errors.New("该规格已存在")
		return
	}
	add := utils.Add{Resouces: model, Table: "spec_item"}
	utils.StartTransaction()
	res, err = add.Insert()
	if err != nil {
		utils.RollbackTransaction()
	} else {
		utils.CommitTransaction()
	}
	return
}

func (SpecService) getItemByWhere(where string) (res int64, err error) {
	var id int64
	query := utils.Query{
		Where:    where,
		Resouces: &id,
		Table:    "spec_item",
	}
	err, r := query.GetOne("id")
	if err != nil || r == false {
		return
	}
	res = id
	return
}

func (s *SpecService) GetItemInfo(id int) (models model.SpecItemCategoryUpdateInfo, err error) {
	cate := model.SpecItemCategory{}
	q := new(utils.Query)
	q.Where = "SI.id = " + common.IntToString(id)
	q.Alias = "SI"
	q.Table = "spec_item"
	q.Resouces = &cate
	join := make([][4]string, 0, 1)
	join = append(join, [4]string{"left", "spec_category", "SC", "SC.id = SI.category_id"})
	q.Join = join
	err, _ = q.GetOne()
	info := model.SpecItemCategoryUpdateInfo{
		SpecItemCategory: cate,
	}
	if err == nil {
		specModelQuery := model.QueryList{}
		specModelQuery.Page = 1
		specModelQuery.PageSize = 100
		res, _ := s.GetCategoryList(&specModelQuery)
		specCategorySlice := (res.List).([]model.SpecModelCategory)
		info.SpecCateGoryList = specCategorySlice
	}
	models = info
	return
}

func (SpecService) UpdateItem(model *model.SpecItem) (res int64, err error) {
	u := new(utils.Update)
	u.Resouces = model
	u.Where = "id = " + common.Int64ToString(model.Id)
	u.Table = "spec_item"
	utils.StartTransaction()
	res, err = u.Update()
	if err != nil {
		utils.RollbackTransaction()
	} else {
		utils.CommitTransaction()
	}
	return
}

func (SpecService) GetItemList(query *model.QueryList) (models *model.QueryList, err error) {
	q := utils.InitQuery()
	if query.PageSize > 0 {
		q.PageSize = query.PageSize
	}
	if query.Page > 0 {
		q.Page = query.Page
	}
	q.Alias = "SI"
	q.Table = "spec_item"
	q.OrderBy = "SI.id desc"
	q.Where = "SI.delete_time = 0"
	specItemSlice := make([]model.SpecItemCategory, 0, q.Page)
	q.Resouces = &specItemSlice
	join := make([][4]string, 0, 1)
	join = append(join, [4]string{"left", "spec_category", "SC", "SC.id = SI.category_id"})
	q.Join = join
	err = q.Select()
	err, count := q.Count()
	query.Count = count
	query.List = specItemSlice
	query.Page = q.Page
	query.PageSize = q.PageSize
	models = query
	return

}

func (s *SpecService) AddModel(model *model.SpecModel) (res int64, err error) {
	model.CreateTime = common.Time()
	res, err = s.getModelByWhere("model_name = '" + model.ModelName + "'")
	if err != nil {
		return
	}
	if res > 0 {
		err = errors.New("该模型已存在")
		return
	}
	add := utils.Add{Resouces: model, Table: "spec_model"}
	utils.StartTransaction()
	res, err = add.Insert()
	if err != nil {
		utils.RollbackTransaction()
	} else {
		utils.CommitTransaction()
	}
	return
}

func (SpecService) getModelByWhere(where string) (res int64, err error) {
	var id int64
	query := utils.Query{
		Where:    where,
		Resouces: &id,
		Table:    "spec_model",
	}
	err, r := query.GetOne("id")
	if err != nil || r == false {
		return
	}
	res = id
	return
}

func (SpecService) UpdateModel(model *model.SpecModel) (res int64, err error) {
	u := new(utils.Update)
	u.Resouces = model
	u.Where = "id = " + common.Int64ToString(model.Id)
	u.Table = "spec_model"
	utils.StartTransaction()
	res, err = u.Update()
	if err != nil {
		utils.RollbackTransaction()
	} else {
		utils.CommitTransaction()
	}
	return
}

func (SpecService) GetModelList(query *model.QueryList) (models *model.QueryList, err error) {
	q := utils.InitQuery()
	if query.PageSize > 0 {
		q.PageSize = query.PageSize
	}
	if query.Page > 0 {
		q.Page = query.Page
	}
	q.Table = "spec_model"
	q.OrderBy = "id desc"
	q.Where = "delete_time = 0"
	specModelSlice := make([]model.SpecModel, 0, q.Page)
	q.Resouces = &specModelSlice
	err = q.Select()
	err, count := q.Count()
	query.Count = count
	query.List = specModelSlice
	query.Page = q.Page
	query.PageSize = q.PageSize
	models = query
	return

}

func (s *SpecService) AddCategory(model *model.SpecCateGory) (res int64, err error) {
	model.CreateTime = common.Time()
	res, err = s.getCategoryByWhere("category_name = '" + model.CategoryName + "' and model_id = " + common.Int64ToString(model.ModelId))
	if err != nil {
		return
	}
	if res > 0 {
		err = errors.New("该属性分类已存在")
		return
	}
	add := utils.Add{Resouces: model, Table: "spec_category"}
	utils.StartTransaction()
	res, err = add.Insert()
	if err != nil {
		utils.RollbackTransaction()
	} else {
		utils.CommitTransaction()
	}
	return
}

func (SpecService) getCategoryByWhere(where string) (res int64, err error) {
	var id int64
	query := utils.Query{
		Where:    where,
		Resouces: &id,
		Table:    "spec_category",
	}
	err, r := query.GetOne("id")
	if err != nil || r == false {
		return
	}
	res = id
	return
}

func (SpecService) UpdateCategory(model *model.SpecCateGory) (res int64, err error) {
	u := new(utils.Update)
	u.Resouces = model
	u.Where = "id = " + common.Int64ToString(model.Id)
	u.Table = "spec_category"
	utils.StartTransaction()
	res, err = u.Update()
	if err != nil {
		utils.RollbackTransaction()
	} else {
		utils.CommitTransaction()
	}
	return
}

func (s *SpecService) GetCategoryInfo(id int) (models model.SpecModelCategoryInfo, err error) {
	cate := model.SpecModelCategory{}
	q := new(utils.Query)
	q.Where = "SC.id = " + common.IntToString(id)
	q.Alias = "SC"
	q.Table = "spec_category"
	q.Resouces = &cate
	join := make([][4]string, 0, 1)
	join = append(join, [4]string{"left", "spec_model", "SM", "SM.id = SC.model_id"})
	q.Join = join
	err, _ = q.GetOne()
	info := model.SpecModelCategoryInfo{
		SpecModelCategory: cate,
	}
	if err == nil {
		specModelQuery := model.QueryList{}
		specModelQuery.Page = 1
		specModelQuery.PageSize = 100
		res, _ := s.GetModelList(&specModelQuery)
		specModels := (res.List).([]model.SpecModel)
		info.SpecModelList = specModels
	}
	models = info
	return
}

func (SpecService) GetCategoryList(query *model.QueryList) (models *model.QueryList, err error) {
	q := utils.InitQuery()
	if query.PageSize > 0 {
		q.PageSize = query.PageSize
	}
	if query.Page > 0 {
		q.Page = query.Page
	}
	q.Alias = "SC"
	q.Table = "spec_category"
	q.OrderBy = "SC.id desc"
	q.Where = "SC.delete_time = 0"
	specCategorySlice := make([]model.SpecModelCategory, 0, q.Page)
	q.Resouces = &specCategorySlice
	join := make([][4]string, 0, 1)
	join = append(join, [4]string{"inner", "spec_model", "SM", "SM.id = SC.model_id"})
	q.Join = join
	err = q.Select()
	err, count := q.Count()
	query.Count = count
	query.List = specCategorySlice
	query.Page = q.Page
	query.PageSize = q.PageSize
	models = query
	return

}
